package com.cn.demo.service.impl;

import com.cn.demo.entity.*;
import com.cn.demo.mapper.*;
import com.cn.demo.service.PageInfoUtilServer;
import com.cn.demo.service.TeachingService;
import com.cn.demo.utils.Result;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class TeachingServiceImpl implements TeachingService {
    @Autowired
    private TeachingMapper teachingMapper;
    @Autowired
    private PageInfoUtilServer pageInfoUtilServer;

    @Autowired
    private CurriculumMapper curriculumMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private WaitJoinCurriculumMapper waitJoinCurriculumMapper;

    @Autowired
    private ExperimentMapper experimentMapper;

    @Autowired
    private EvaluateMapper evaluateMapper;

    @Autowired
    private InternshipMapper internshipMapper;

    @Autowired
    private DesignTeamMapper designTeamMapper;

    @Autowired
    private DesignMapper designMapper;

    @Autowired
    private InternshipLogMapper internshipLogMapper;

    @Override
    public Result organizationAddStudent(String studentId,String organizationId) {
        if (studentMapper.getByStudentId(studentId) == null){
            return Result.error("学生不存在");
        }
        Curriculum curriculum = curriculumMapper.infoByOrganizationId(organizationId);
        if (curriculum==null){
            return Result.error("课程不存在");
        }
        WaitJoinCurriculum waitJoinCurriculum = new WaitJoinCurriculum(curriculum.getCurriculumId(),studentId);
        if (curriculumMapper.isJoin(waitJoinCurriculum)>0){
            return Result.error("该学生加入该课程");
        }

        waitJoinCurriculumMapper.delete(waitJoinCurriculum);

        if (teachingMapper.organizationAddStudent( studentId, organizationId)>0){
            return Result.success();
        }
        return Result.error();
    }

    @Override
    public Result getOrganizationStudentByOrganizationId(Integer page, Integer limit, String organizationId, String studentId, String studentName) {
        PageHelper.startPage(page, limit);
        List<Student>  studentList = teachingMapper.getOrganizationStudentByOrganizationId(organizationId,studentId,studentName);
        return  Result.success(pageInfoUtilServer.currency(studentList));
    }

    @Override
    public Result organizationStudentDelete(List<String> studentIdList,String organizationId) {
        /**
         * 判断是否开始了课程
         */
        Curriculum curriculumItem = curriculumMapper.getCurriculumByOrganizationId(organizationId);
        for (String studentItem : studentIdList) {
            experimentMapper.delete(curriculumItem.getCurriculumExperimentId(),studentItem);
            evaluateMapper.delete(curriculumItem.getCurriculumExperimentId(),studentItem);
            //删除实训
            Internship internship = internshipMapper.getInternshipCorporation(studentItem, curriculumItem.getCurriculumInternshipId());
            if (internship!=null){
                internshipLogMapper.delete(internship.getInternshipId());
                internshipMapper.deleteByPrimaryKey(internship.getInternshipId());
            }
            evaluateMapper.delete(curriculumItem.getCurriculumInternshipId(),studentItem);
            //删除课程设计
            Design designSubject = designMapper.getDesignSubject(studentItem, curriculumItem.getCurriculumDesignId());
            if(designSubject!=null){
                List<DesignTeam> designTeamList = designSubject.getDesignTeamList();
                designTeamMapper.delete(designSubject.getDesignId(),studentItem);
                evaluateMapper.delete(curriculumItem.getCurriculumDesignId(),studentItem);
                if (designTeamList.size()==1){
                    //没有人直接删除设计
                    designMapper.deleteByPrimaryKey(designSubject.getDesignId());
                }
            }
            List<String> StringListStudent = new ArrayList<>();
            StringListStudent.add(studentItem);
            //删除班级学生
            teachingMapper.organizationStudentDelete(StringListStudent,organizationId);
        }
        return Result.success();
    }


    @Override
    public Result joinCurriculum(String studentId, String organizationId) {
        //后续这个需要做判断
        if (teachingMapper.joinCurriculum(studentId,organizationId)>0){
            return Result.success();
        }
        return Result.error();
    }


}